package com.base.cn.platform.os.controller.qa.qaadd;

import com.base.cn.platform.os.common.controller.BaseController;
import com.base.cn.platform.os.common.utils.DataUtil;
import com.base.cn.platform.os.common.utils.ObjectUtils;
import com.base.cn.platform.os.common.utils.StringUtils;
import com.base.cn.platform.os.common.utils.result.ResultUtil;
import com.base.cn.platform.os.entity.qa.qaadd.QaAdd;
import com.base.cn.platform.os.entity.qa.question.QaQuestion;
import com.base.cn.platform.os.service.qa.qaadd.QaAddBiz;
import com.base.cn.platform.os.service.qa.question.QaQuestionBiz;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

/**
 * 追问Controller
 * @author LIZHIPENG
 * @since 2018/4/26
 */
@RestController
@RequestMapping("/course")
public class QaAddController extends BaseController {

    @Autowired
    private QaAddBiz qaAddBiz;
    @Autowired
    private QaQuestionBiz qaQuestionBiz;

    /**
     * 保存问题追问
     * @param qaAdd 追问信息
     * @param userId 追问人ID
     * @return Map<String, Object>
     */
    @RequestMapping("/saveQuestionAdd")
    public Map<String, Object> saveQuestionAdd(@RequestBody QaAdd qaAdd,
                                               @RequestParam("userId") BigDecimal userId){
        if(!DataUtil.idIsNotNull(userId)){
            return ResultUtil.ERROR("请登录后再来追问");
        }
        if(ObjectUtils.isEmpty(qaAdd)){
            return ResultUtil.ERROR("系统繁忙，请稍后重试");
        }
        if(!DataUtil.idIsNotNull(qaAdd.getQuestionId())){
            return ResultUtil.ERROR("问题ID为空，保存失败");
        }
        QaQuestion question = qaQuestionBiz.findQuestionById(qaAdd.getQuestionId(), false, false, false, false, false, false, false);
        if(ObjectUtils.isEmpty(question)){
            return ResultUtil.ERROR("系统繁忙，请稍后重试");
        }
        if(userId.compareTo(question.getUserId()) !=0){
            return ResultUtil.ERROR("此问题非您所提，您不能追问");
        }
        if(StringUtils.isEmpty(qaAdd.getAddContext()) || "".equals(qaAdd.getAddContext().trim())){
            return ResultUtil.ERROR("追问内容不能为空");
        }
        qaAdd.setAddContext(qaAdd.getAddContext().trim());
        if(qaAdd.getAddContext().length()>200){
            return ResultUtil.ERROR("追问内容不能超过200个字");
        }
        //查询该问题的追问次数
        QaAdd _qaAdd = new QaAdd();
        _qaAdd.setQuestionId(qaAdd.getQuestionId());
        BigDecimal addCount = qaAddBiz.getCount(_qaAdd);
        if(new BigDecimal(2).compareTo(addCount) <= 0){
            return ResultUtil.ERROR("该问题追问次数已用完，不能再追问了");
        }

        qaAddBiz.saveOrUpdateQuestionAdd(qaAdd);

        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("success", true);
        resultMap.put("message", "保存成功");
        resultMap.put("entity", qaAdd);
        resultMap.put("addCount", addCount.intValue() + 1 );
        return resultMap;
    }

    /**
     * 后台保存追答
     * @param qaAdd 追答数据
     * @return Map<String, Object>
     */
    @RequestMapping("/saveQuestionAddAnswer")
    public Map<String, Object> saveQuestionAddAnswer(@RequestBody QaAdd qaAdd){
        if(ObjectUtils.isEmpty(qaAdd) || !DataUtil.idIsNotNull(qaAdd.getId())){
            return ResultUtil.ERROR("系统繁忙，请稍后重试");
        }
        if(StringUtils.isEmpty(qaAdd.getAddAnswer()) || "".equals(qaAdd.getAddAnswer().trim())){
            return ResultUtil.ERROR("追答内容不能为空，请重新输入");
        }
        qaAdd.setAddAnswer(qaAdd.getAddAnswer().trim());
        qaAddBiz.saveOrUpdateQuestionAdd(qaAdd);
        return ResultUtil.SUCCESS("保存成功");
    }
}
